<?php

namespace app\admin\controller\Business;

use app\common\controller\Backend;

/**
 * 客户管理
 *
 * @icon fa fa-circle-o
 */
class Info extends Backend
{

    protected $model = null;

    //关联搜索
    protected $relationSearch = true;

    public function _initialize()
    {
        parent::_initialize();

        $this->model = model('Business.Business');
    }

    public function index($ids = NULL)
    {
        $row = $this->model->with(['source', 'admin', 'parent'])->find($ids);

        if (!$row) {
            $this->error(__('No results were found'));
            exit;
        }

        $this->assign('row', $row);

        return $this->view->fetch();
    }

    // 申请列表
    public function receive($ids = null)
    {
        $this->model = model('Business.Receive');

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);

        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $AuthWhere = ['busid' => $ids];

            $total = $this->model
                ->with(['admin'])
                ->where($where)
                ->where($AuthWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();

            $list = $this->model
                ->with(['admin'])
                ->where($where)
                ->where($AuthWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        return $this->view->fetch();
    }

    //回访记录
    public function visit($ids = null)
    {
        $this->model = model('Business.Visit');

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);

        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $AuthWhere = ['busid' => $ids];

            $total = $this->model
                ->with(['admin', 'business'])
                ->where($where)
                ->where($AuthWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();

            $list = $this->model
                ->with(['admin', 'business'])
                ->where($where)
                ->where($AuthWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
    }

    // 消费记录列表
    public function record($ids = null)
    {
        $this->model = model('Business.Record');

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);

        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $AuthWhere = ['busid' => $ids];

            $total = $this->model
                ->where($where)
                ->where($AuthWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();

            $list = $this->model
                ->where($where)
                ->where($AuthWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        return $this->view->fetch();
    }

    // 课程订单列表
    public function subject($ids = null)
    {
        $this->model = model('Subject.Order');

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);

        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $AuthWhere = ['busid' => $ids];

            $total = $this->model
                ->with(['subject'])
                ->where($where)
                ->where($AuthWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();

            $list = $this->model
                ->with(['subject'])
                ->where($where)
                ->where($AuthWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        return $this->view->fetch();
    }
}
